﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using System.Collections.ObjectModel;
using ActivityTracker.Library;
using Common.Library;

namespace ActivityTracker
{
    public partial class ActivityAndScheduleSelection : PhoneApplicationPage
    {
        #region Global

        ActivityData objActivityData = new ActivityData();
        Actvity objActivity = new Actvity();
        Schedules objSchedules = new Schedules();
        int count = 20;

        #endregion

        #region Constructor

        public ActivityAndScheduleSelection()
        {
            InitializeComponent();
            objSchedules.UpdateCurrentSchedules();
        }

        #endregion

        #region Page events

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
               
                List<ActivityProp> objActivities = objActivity.LoadActivities(count);
                lbxActivities.ItemsSource = objActivities;
                List<ActivityProp> objScheds = objSchedules.LoadSchedsOfCurrentDate();
                lbxScheds.ItemsSource = objScheds;
            }
            catch (Exception ex)
            {
                string mess = "Exception in PhoneApplicationPage_Loaded Method In ActivityAndScheduleSelection file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void AppBarNew_Click(object sender, EventArgs e)
        {
            try
            {
                objActivityData.ActivityOldDataClearing();
                NavigationService.Navigate(new Uri("/Views/Activities/AddNewActivity.xaml?flag=ActSelection", UriKind.Relative));
            }
            catch (Exception ex)
            {
                string mess = "Exception in AppBarNew_Click Method In ActivityAndScheduleSelection file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void lbxActivities_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxActivities.SelectedIndex == -1)
                    return;
                if ((lbxActivities.SelectedItem as ActivityProp).ActName != "get more")
                    NavigationService.Navigate(new Uri("/StartActivity.xaml?actId=" + (lbxActivities.SelectedItem as ActivityProp).ActId, UriKind.Relative));
                else
                {
                    count = count + 20;
                    lbxActivities.ItemsSource = objActivity.LoadActivities(count);
                    lbxActivities.ScrollIntoView(lbxActivities.Items[count - 20]);
                }
            }
            catch (Exception ex)
            {
                string mess = "Exception in lbxActivities_SelectionChanged Method In ActivityAndScheduleSelection file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void lbxScheds_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxScheds.SelectedIndex == -1)
                    return;
                NavigationService.Navigate(new Uri("/StartActivity.xaml?actId=" + (lbxScheds.SelectedItem as ActivityProp).ActId, UriKind.Relative));
            }
            catch (Exception ex)
            {
                string mess = "Exception in lbxScheds_SelectionChanged Method In ActivityAndScheduleSelection file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        #endregion
    }
}